<!---

CF_Kalendar

COPYRIGHT & LICENSING INFO
-------------------------------------------------------------------

Copyright 2006 TJ Downes - tdownes@sanative.net - http://www.sanative.net

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

--->
<cfswitch expression="#appAction#">
	<cfcase value="displayImportForms">
	<cfform action="index.cfm?appAction=displayImportData" name="importForm" enctype="multipart/form-data">
	<table>
	<tr>
		<td style="text-align:right;">File:&nbsp;</td>
		<td style="text-align:left;"><cfinput type="file" required="true" validate="noblanks" message="File is a required field. Use the Browse button to select a file from your computer to upload." id="myImportFile" name="myImportFile" size="22" class="inputtextfield" /></td>
	</tr>
	<tr>
		<td style="text-align:right;">Type:&nbsp;</td>
		<td style="text-align:left;"><cfinput type="radio" name="ImportType" id="iCal" required="true" message="You must select the type of file you are importing." value="iCal"/> iCal <cfinput type="radio" name="ImportType" id="OutlookCSV" required="true" message="You must select the type of file you are importing." value="OutlookCSV"/> Outlook CSV</td>
	</tr>
	<tr>
		<td colspan="2" style="text-align: center;"><input type="submit" name="submit" value="preview import"/></td>
	</tr>
	</table>
	</cfform>
	</cfcase>
	<cfcase value="displayImportData">
	<!--- upload file, read data, delete file, display data --->
	<cfswitch expression="#form.ImportType#">
		<cfcase value="iCal">
		<cfset variables.acceptType = "application/octet-stream,text/calendar" />
		</cfcase>
		<cfcase value="OutlookCSV">
		<cfset variables.acceptType = "application/vnd.ms-excel" />
		</cfcase>
	</cfswitch>
	<cftry>
	<cffile action="upload" filefield="myImportFile" nameconflict="overwrite" accept="#variables.acceptType#" destination="#KalendarConfig.getTmpImportDirectory()#" />
	<cffile action="read" file="#KalendarConfig.getTmpImportDirectory()##CFFile.ServerFile#" variable="importfile" />
	<cffile action="delete" file="#KalendarConfig.getTmpImportDirectory()##CFFile.ServerFile#" />
		<cfcatch type="any">
			<strong style="font-size: 1.5em;">An error occured</strong><br />
			You may have tried to upload a file which was corrupt or not of the correct type. Please use <a href="http://<cfoutput>#cgi.server_name##KalendarConfig.getKalendarAdminPath()#</cfoutput>index.cfm?appAction=displayImportForms" style="text-decoration: underline;">this link</a> to try again.
			<cfabort />
		</cfcatch>
	</cftry>
	<cfswitch expression="#form.ImportType#">
		<cfcase value="iCal">
		<cfscript>
			ical = createObject("component","#KalendarConfig.getCFCPath()#ical").init(importfile);
			results = ical.getEvents();
			ImportData = ArrayNew(1);
			ArrayCount = 0;
		</cfscript>
		<div style="width: 590px;">
		<cfform name="dataImportForm" action="#cgi.SCRIPT_NAME#" method="post">
		<cfinput type="hidden" name="appAction" value="importData"/>
		<input type="button" name="selectAll" value="select/deselect all" onclick="toggleList()">
		<cftry>
		<cfloop index="x" from="1" to="#arraylen(results)#">
			<cfset ArrayCount = ArrayCount + 1 />
			<cfset eventData = results[x]/>
			<cfoutput>
			<cfif NOT structKeyExists(eventData,"summary")>
				<div style="background-color: ##ccc; font-weight: bold; font-size: 12pt;"><cfinput type="checkbox" name="#x#" value="1"/> Untitled Event</div>
				<cfset Title = "Untitled Event" />
			<cfelse>
				<div style="background-color: ##ccc; font-weight: bold; font-size: 12pt;"><cfinput type="checkbox" name="#x#" value="1"/> #eventData.summary.data#</div>
				<cfset Title = Trim(Replace(eventData.summary.data, "\", "", "ALL")) />
			</cfif>
			<cfset startDate = ical.icalParseDateTime(eventData.dtstart.data)/>
			<cfif structKeyExists(eventData,"dtend")>
				<cfset endDate = ical.icalParseDateTime(eventData.dtend.data)/>
			<cfelseif structKeyExists(eventData,"duration") and eventData.duration.data is not "P1D">
				<cfset endDate = ical.icalParseDuration(eventData.duration.data,startdate)/>
			<cfelse>
				<cfset enddate = startdate/>
			</cfif>
			#dateFormat(startDate,"mm/dd/yy")#<cfif dateFormat(startDate,"mm/dd/yy") neq dateFormat(endDate,"mm/dd/yy")> - #dateFormat(endDate,"mm/dd/yy")#</cfif><br />
			<cfif structKeyExists(eventData,"description")>#Trim(Replace(Replace(eventData.description.data, "\n","<br />", "ALL"), "\", "", "ALL"))#
			<cfset Description = Trim(Replace(Replace(eventData.description.data, "\n","<br />", "ALL"), "\", "", "ALL")) />
			<cfelse>
			<cfset Description = "Description unavailable" />
			</cfif>
			<p/>
			</cfoutput>
			<cfif TimeFormat(StartDate, "h:mm tt") neq "12:00 AM" AND TimeFormat(EndDate, "h:mm tt") neq "12:00 AM">
				<cfset StartTime = TimeFormat(StartDate, "h:mm tt") />
				<cfset EndTime = TimeFormat(EndDate, "h:mm tt") />
			<cfelse>
				<cfset StartTime = "" />
				<cfset EndTime = "" />
			</cfif>
			<cfscript>
				ImportData[ArrayCount] = StructNew();
				ImportData[ArrayCount].Title = Title;
				ImportData[ArrayCount].StartDate = StartDate & ' ' & StartTime;
				ImportData[ArrayCount].EndDate = EndDate & ' ' & EndTime;
				ImportData[ArrayCount].Description = Description;
			</cfscript>
		</cfloop>
			<cfcatch type="any">
			There was an error parsing your import file. It may be corrupt.Please use <a href="http://<cfoutput>#cgi.server_name##KalendarConfig.getKalendarAdminPath()#</cfoutput>/kalendar/index.cfm?appAction=displayImportForms" style="text-decoration: underline;">this link</a> to try again.
			<cfabort />
			</cfcatch>
		</cftry>
		<cfset session.results = ImportData />
		<input type="submit" name="importEvents" value="import selected events"> <input type="button" name="cancelImport" value="cancel">
		</cfform>
		</div>
		</cfcase>
		<cfcase value="OutlookCSV">
			<cftry>
			<cfset OutlookCalendar = createObject("component","#KalendarConfig.getCFCPath()#OutlookCalendar").init(importfile)/>
			<cfset session.results = OutlookCalendar.getEvents()/>
				<cfcatch type="any">
				There was an error parsing your import file. It may be corrupt.Please use <a href="http://<cfoutput>#cgi.server_name##KalendarConfig.getKalendarAdminPath()#</cfoutput>/kalendar/index.cfm?appAction=displayImportForms" style="text-decoration: underline;">this link</a> to try again.
				<cfabort />
				</cfcatch>
			</cftry>
			<div style="width: 590px;">
			<cfform name="dataImportForm" action="#cgi.SCRIPT_NAME#" method="post">
			<cfinput type="hidden" name="appAction" value="importData"/>
			<input type="button" name="selectAll" value="select/deselect all" onclick="toggleList()">
			<cfloop index="x" from="1" to="#arraylen(session.results)#">
			<cfoutput>
			<div style="background-color: ##ccc; font-weight: bold; font-size: 12pt;"><cfinput type="checkbox" name="#x#" value="1"/> #session.results[x].Title#</div>
			<p />
			#session.results[x].StartDate#<cfif dateFormat(session.results[x].StartDate,"mm/dd/yy") neq dateFormat(session.results[x].EndDate,"mm/dd/yy")> - #dateFormat(session.results[x].EndDate,"mm/dd/yy")#</cfif><br />
			#session.results[x].Description#
			</cfoutput>
			</cfloop>
			<input type="submit" name="importEvents" value="import selected events"> <input type="button" name="cancelImport" value="cancel">
			</cfform>
			</div>
		</cfcase>
	</cfswitch>
	</cfcase>
</cfswitch>